package com.cqjtu.store.address.controller;

import com.cqjtu.store.address.pojo.dto.AddressDTO;
import com.cqjtu.store.address.pojo.vo.AddressUpdateVO;
import com.cqjtu.store.address.pojo.vo.AddressVO;
import com.cqjtu.store.address.pojo.vo.DistrictVO;
import com.cqjtu.store.address.service.AddressService;
import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.R;
import com.cqjtu.store.user.pojo.dto.UserLoginDTO;
import com.cqjtu.store.user.pojo.dto.UserUpdateDTO;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.PushBuilder;
import java.util.List;

@RestController
@RequestMapping("api/address")
@Slf4j
// todo 文档注解
@Api(tags = "地址模块")
public class AddressController {

    @Autowired
    private AddressService addressService;
    /**
     * 查询当前用户的地址列表
     * @param request
     * @return
     */
    @GetMapping("list")
    @ApiOperation("查询当前用户的地址列表")
    public R<List<AddressVO>> getAddressList(HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //查询地址列表
        List<AddressVO> addressVOList = addressService.getAddressList(userLoginVO);
        return R.ok(addressVOList);
    }

    /**
     * 删除地址信息
     * @param id
     * @param request
     * @return
     */
    @DeleteMapping("delete/{id}")
    @ApiOperation("删除地址信息")
    public R<Void> delete(@PathVariable Integer id, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        log.debug("用户信息:{}",userLoginVO);
        addressService.delete(id,userLoginVO);
        return R.ok();
    }

    /**
     * 设置地址默认
     * @param id 地址编号
     * @param request
     * @return
     */
    @PutMapping("setDefault/{id}")  //setDefault?id=1001 => setDefalut/1001
    @ApiOperation("设置地址默认")
    public R<Void> setDefault(@PathVariable Integer id,HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        addressService.setDefault(id,userLoginVO);
        return R.ok();
    }

    /**
     * 查询行政地址
     * @param parent 父级行政地址的编号
     * @return
     */
    @GetMapping("district") // /api/address/district
    @ApiOperation("查询行政地址")
    public R<List<DistrictVO>> getDistrictList(Integer parent){
        List<DistrictVO> districtList = addressService.getDistrictList(parent);
        return R.ok(districtList);
    }


    @PostMapping("new")
    public R<Void> saveAddress(@RequestBody @Validated AddressDTO addressDTO, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        addressService.saveAddress(addressDTO,userLoginVO);
        return R.ok();
    }

    @GetMapping("{id}") // /api/address/1001 restful
    public R<AddressUpdateVO> getAddresById(@PathVariable Integer id, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //查询
        AddressUpdateVO addressUpdateVO = addressService.getAddressById(id,userLoginVO);
        return R.ok(addressUpdateVO);
    }

    /**
     *修改地址
     * 请求方式：
     * get: 查询
     * post: 新增
     * put: 修改
     * delete: 删除
     * header
     * @param addressDTO
     * @param request
     * @return
     */
    @PutMapping("update")
    public R<Void> updateAddress(@RequestBody @Validated AddressDTO addressDTO,HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        addressService.update(addressDTO,userLoginVO);
        return R.ok();
    }

}
